package com.nut.lmsc.api.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nut.lmsc.api.req.ShopCheckReq;
import com.nut.lmsc.api.req.ShopQueryReq;
import com.nut.lmsc.common.Holder;
import com.nut.lmsc.common.Result;
import com.nut.lmsc.entity.Shop;
import com.nut.lmsc.interceptor.AdminJwt;
import com.nut.lmsc.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/admin/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @GetMapping(value = "list")
    public Result<IPage<Shop>> list(ShopQueryReq req) {
        return shopService.listPage(req);
    }

    @GetMapping(value = "/{id}")
    public Result<Shop> detail(@PathVariable Integer id) {
        return shopService.getById(id);
    }

    @PostMapping
    public Result add(@RequestBody Shop shop) {

        //自营店铺
        shop.setMemberId(0L);
        shop.setOwn(1);

        return shopService.add(shop);
    }

    @PostMapping("/check")
    public Result check(@RequestBody ShopCheckReq req) {
        AdminJwt adminJwt = Holder.ADMIN_JWT_THREAD_LOCAL.get();
        req.setUserId(adminJwt.getUserId());
        req.setUserName(adminJwt.getUsername());
        return shopService.check(req);
    }

    @PutMapping
    public Result update(@RequestBody Shop shop) {
        return shopService.updateById(shop);
    }

    @PostMapping("/authCircle")
    public Result authCircle(@RequestBody Shop shop) {
        return shopService.authCircle(shop);
    }

    @DeleteMapping
    public Result delete(@RequestBody Set<Integer> ids) {
        return shopService.deleteByIds(ids);
    }

}